mui.init();
/**
 * 当点击首页是调用“刷新”事件 
 */
window.addEventListener('reload',function(event){
	loadUserInfo();
});

	var sumamounts="";
	var name="";
mui.plusReady(function() {
	var ele_btn_login = CmwMcore.$('btn_login');
	MeventMgr.onTouchstart(ele_btn_login,function(event,self){
		plus.nativeUI.confirm("确定要退出吗?",function(e){
			var index = e.index;
			if(index != 0) return;
			loginout();
		});
	});
		
	function toLogin(){
		plus.webview.currentWebview().parent().evalJS("openTab(null,subpages2[0]);");
	}
	function loginout(){
		var url = UrlConfig.muser_loginout;
		var indexPage = plus.webview.getLaunchWebview();
		//alert(indexPage);
		var indexPageId = indexPage.id;/*首页WebView对象ID*/
		var sessionId = plus.storage.getItem(KeyCfg.JSESSIONID);
		var userId = plus.storage.getItem(KeyCfg.userId);
		var pars = {sessionId:sessionId,userId:userId};
		$.ajax(url, {
	        data : pars,  
	        dataType : 'jsonp',  
	        crossDomain: true,  
	        success: function(data) {
	        		data = JSON.parse(data);
	        		plus.storage.removeItem(KeyCfg.JSESSIONID);
	        		var settings = app.getSettings();
	        		settings.gestures = '';
					settings.autoLogin = false;
					settings.overTime = 0;
					app.setSettings(settings);
	        		plus.storage.setItem(KeyCfg.currClickTabId,"defaultTab");
				mui.fire(indexPage,'refresh',{});
	        }
	    });
	}
	
	mui(".mui-content ").on("tap", "a", function() {
	var targetPageId = plus.webview.currentWebview().id;
	plus.storage.setItem(KeyCfg.targetPageId,targetPageId);
	var self = this;
	var name=$(self).attr("id");
	switch(name){
		case 'btn_recharge':{/*充值*/
			var targetPageId = plus.webview.currentWebview().id;
			plus.storage.setItem(KeyCfg.targetPageId,targetPageId);
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.checkSecuityForwad(function(){
					openPage('../recharge/recharge.html',{});	
				});
			},toLogin);
			break;
		}case 'btn_withdraw':{/*提现*/
			var targetPageId = plus.webview.currentWebview().id;
			plus.storage.setItem(KeyCfg.targetPageId,targetPageId);
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.checkSecuityForwad(function(){
			    	openPage('../anquanyz/anquanyz.html',{});	
				});
			},toLogin);
			break;
		}
		case 'my_invest':
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.open('../myInvest/myInvest.html',{});
			},toLogin);
			break;
		case 'invest_return':
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.open('../redeem/redeem.html',{});
			},toLogin);
			break;
		case 'member_transfer':
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.open('../userFund/userFund.html',{});
			},toLogin);
			break;	
		case 'bank_card':
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.open('../mybank/mybank.html',{});
			},toLogin);
			break;
		case 'my_hongbao':
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.open('../hongbao/myHongBao.html',{});
			},toLogin);
			break;	
		case 'auto_tender':
			var targetPageId = plus.webview.currentWebview().id;
			plus.storage.setItem(KeyCfg.targetPageId,targetPageId);
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.checkSecuityForwad(function(){
			    	openPage('../automatic/autoBid.html',{});	
				});
			},toLogin);
			break;
		case 'btn_person':
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.open('../personalcenter/personal_info.html',{});
			},toLogin);
			break;	
		case 'asset_count':
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.open('../assetCount/assetCount.html',{});
			},toLogin);
			break;	
		case 'common_problem':
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.open('../commonproblem/cproblem.html',{});
			},toLogin);
			break;	
		case 'trade_record':
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.open('../tradeRecord/tradeRecord.html',{});
			},toLogin);
		    break;	
		 case 'common_frend':
			var targetPageId = plus.webview.currentWebview().id;
			plus.storage.setItem(KeyCfg.targetPageId,targetPageId);
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.checkSecuityForwad(function(){
			    openPage('../share/share.html',{});	
				});
			},toLogin);
			break;
		case 'safe_center':
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.open('../personalcenter/personal_info.html',{});
			},toLogin);
			break;	
		}
});

	/**
	 * 打开新页面 
 	 * @param {Object} url	新地址
	 * @param {Object} pars	参数
	 */
	function openPage(url ,pars){
		if(!pars) pars = {};
		var cfg = {pars : pars};
		MuiEventMgr.openBySafe(url,cfg);
	}
//	不同分辨率手机按比例显示
    //alert($(window).height());
	var height1 = $(window).height()*0.105+"px";    //九宫格显示长度
	 $(".app_member_fun").css("height",height1);	
	 
	 var height2 = $(window).height()*0.20+"px";    //帐户总资产整个横条长度
	 $(".app_member_header").css("height",height2);	
	 
	 var height3 = $(window).height()*0.063+"px";    //可用余额，累计收益长度
	 $(".app_member_amountInfo").css("line-height",height3);	
	 
	 var height4 = $(window).height()*0.11+"px";    //充值提现按钮长度
	 $(".cztx").css("height",height4);	
	 
	 var height5 = $(window).height()*0.04+"px";    //帐户总资产长度
	 //alert(height5);
	 $(".allAsset").css("padding-top",height5);	
	 
	 var height6 = $(window).height()*0.02+"px";    //帐户总资产数字长度
	 $(".show_allAsset").css("padding-top",height6);	
	 
	 var height7 = $(window).height()*0.08+"px";    //可用余额，累计收益TR高度
	 $(".cfn-table-tr").css("height",height7);	
});

	/**
	 * 加载金额汇总信息 
	 */
	function loadUserInfo(){
		var url = UrlConfig.mmember_myindex;
		var sessionId = plus.storage.getItem(KeyCfg.JSESSIONID);
		var userId = plus.storage.getItem(KeyCfg.userId);
		var pars = {sessionId:sessionId,userId:userId};
		plus.nativeUI.showWaiting("数据加载中...",{modal:true});
		$.ajax(url, {  
	        data : pars,  
	        dataType : 'jsonp',  
	        crossDomain: true,  
	        success: function(data) {
	        	data = JSON.parse(data);
	        	callback(data);
	        	addRealNameListener();
				plus.nativeUI.closeWaiting();
	        }
	    });
	}
	
	/**
	 * 填充页面数据 
	 * @param {Object} data
	 */
	function callback(data){
		var phone = data.phone;
		if(!phone) {
			phone='未手机验证';
	    }
//		name=realName;
		var userName = plus.storage.getItem(KeyCfg.userName);
		var summoneyAll = data.summoneyAll || 0.0;
		summoneyAll = CmwMcore.getThousandths(summoneyAll);
		var summoney = data.summoney || 0.0;
		sumamounts =data.sumamount;
		
		summoney = CmwMcore.getThousandths(summoney);
		var totalmoney = data.totalmoney || 0.0;
		totalmoney = CmwMcore.getThousandths(totalmoney);
		var lj_interests = data.lj_interests || 0.0;
		lj_interests = CmwMcore.getThousandths(lj_interests);
		if(phone == '未手机验证') {
			MuiEventMgr.checkSecuity();
		}
//		realName = "<a id='realName_security' href='javascript:void(0)'>未认证&nbsp;&nbsp;</a>";
//		$('#realName').html(realName);
		//$('#userName').html("("+userName+")");
		$('#userName').html(userName+"的账户");
		$('#lj_interests').html("￥"+lj_interests);
		$('#summoneyAll').html("￥"+summoneyAll);
		$('#totalmoney').html("￥"+totalmoney);
		$('#summoney').html("￥"+summoney);
	}
	
	/*---- 实名认证链接 ----*/
	function addRealNameListener(){
		var ele_realName_security = CmwMcore.$('realName_security');
		if(!ele_realName_security) return;
		MeventMgr.onTouchstart(ele_realName_security,function(event,self){
			var targetPageId = plus.webview.currentWebview().id;
			plus.storage.setItem(KeyCfg.targetPageId,targetPageId);
			MuiEventMgr.checkSecuity();
		});
	}
	
	function allAsset(){
	mui.alert("账户总资产 = 可用余额 + 投资中冻结金额+提现中冻结金额+预约中冻结金额+待收本金+待收收益");
}
	function income(){
	mui.alert("投资人在来理财所获得的所有投资收益以及其他收益(包括但不限于罚息、推广活动收入等)");
}
	function balance(){
	mui.alert("账户内投资人可自由支配的资金\n");
}
